package com.example.chatplatform.controller;


import com.example.chatplatform.model.dto.CreateGroupDTO;
import com.example.chatplatform.model.dto.GroupAnmDTO;
import com.example.chatplatform.model.dto.GroupManagerDTO;
import com.example.chatplatform.model.entity.User;
import com.example.chatplatform.model.vo.*;
import com.example.chatplatform.result.Result;
import com.example.chatplatform.service.FileOssService;
import com.example.chatplatform.service.GroupService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    public GroupService groupService;

    @Autowired
    public FileOssService fileOssService;

    //创建群聊
    @PostMapping("/createGroup")
    private Result createGroup(@RequestParam("avayar") MultipartFile file, @RequestParam("group") String groupJson) throws JsonProcessingException {
        CreateGroupDTO createGroupDTO = new ObjectMapper().readValue(groupJson, CreateGroupDTO.class);
        Integer groupId = groupService.groupAccount(createGroupDTO);
        if(groupId == null) {
            String FileUrl = fileOssService.uploadToOss(file);
            Integer groupId1 = groupService.createGroup(createGroupDTO, FileUrl);
            if(groupId1 == null){
                return Result.error("创建失败");
            } else {
                return Result.success(groupId1);
            }
        } else {
            return Result.error("已有该群号");
        }
    }

    //查询群成员
    @GetMapping("/groupMember")
    private Result groupMember(@RequestParam Integer groupId) {
        List<GroupMemberVO> groupMemberVOList = groupService.groupMember(groupId);
        if (groupMemberVOList != null) {
            return Result.success(groupMemberVOList);
        } else {
            return Result.error("查询失败");
        }
    }

    //查询群公告
    @GetMapping("/groupAnm")
    private Result groupAnm(@RequestParam Integer groupId) {
        List<GroupAnmVO> groupAnmVOList = groupService.groupAnm(groupId);
        if (groupAnmVOList != null) {
            return Result.success(groupAnmVOList);
        } else {
            return Result.error("查询失败");
        }
    }

    //发布群公告
    @PostMapping("/addGroupAnm")
    private Result addGroupAnm(@RequestBody GroupAnmDTO groupAnmDTO) {
        if (groupService.addGroupAnm(groupAnmDTO)) {
            return Result.success("发布成功");
        } else {
            return Result.error("发布失败");
        }
    }

    //设置管理员
    @PostMapping("/setGroupManager")
    private Result setGroupManager(@RequestBody GroupManagerDTO groupManagerDTO) {
        if (groupService.setGroupManager(groupManagerDTO)) {
            return Result.success("设置成功");
        } else {
            return Result.error("设置失败");
        }
    }

    //取消管理员
    @PostMapping("/cancelGroupManager")
    private Result cancelGroupManager(@RequestBody GroupManagerDTO groupManagerDTO) {
        if (groupService.cancelGroupManager(groupManagerDTO)) {
            return Result.success("设置成功");
        } else {
            return Result.error("设置失败");
        }
    }

    //请出成员
    @DeleteMapping("/removeMember")
    private Result removeMember(@RequestBody GroupManagerDTO groupManagerDTO) {
        if (groupService.removeMember(groupManagerDTO)) {
            return Result.success("请出成功");
        } else {
            return Result.error("请出失败");
        }
    }

    //查询群聊通知
    @GetMapping("/getGroupInform")
    private Result getGroupInform(@RequestParam Integer userId) {
        List<GroupInformVO> groupInformVOS = groupService.getGroupInform(userId);
        if (groupInformVOS != null)  {
            return Result.success(groupInformVOS);
        } else {
            return Result.success("查询失败");
        }
    }

    //查询群聊详情信息
    @GetMapping("/getGroupInfo")
    private Result getGroupInfo(@RequestParam Integer userId, @RequestParam Integer groupId) {
        GroupDetailVO groupDetailVO = groupService.getGroupInfo(userId, groupId);
        if (groupDetailVO != null) {
            return Result.success(groupDetailVO);
        } else {
            return Result.error("查询失败");
        }
    }

    //加入群聊
    @PostMapping("/joinGroup")
    private Result joinGroup(@RequestBody GroupManagerDTO groupManagerDTO) {
        if (groupService.joinGroup(groupManagerDTO)) {
            return Result.success("加入成功");
        } else {
            return Result.error("加入失败");
        }
    }

    //退出群聊
    @DeleteMapping("/quitGroup")
    private Result quitGroup(@RequestBody GroupManagerDTO groupManagerDTO) {
        if (groupService.quitGroup(groupManagerDTO)) {
            return Result.success("退出成功");
        } else {
            return Result.error("退出失败");
        }
    }

    //解散群聊
    @DeleteMapping("/disbandGroup")
    private Result disbandGroup(@RequestBody GroupManagerDTO groupManagerDTO) {
        if (groupService.disbandGroup(groupManagerDTO)) {
            return Result.success("解散成功");
        } else {
            return Result.error("解散失败");
        }
    }

}
